part of '../repository.dart';

const _kCustomCollectionTypeValue = 'custom_collection';

class _CustomCollectionDto extends Equatable
    implements _CustomCollectionListItemDto, _CollectionDto {
  const _CustomCollectionDto({
    required this.id,
    required this.title,
    required this.lexicalItemIds,
    required this.selected,
    required this.highPriority,
    required this.sortingDto,
  });

  @override
  final String id;

  @override
  final String title;

  @override
  final BuiltSet<String> lexicalItemIds;

  @override
  final bool selected;

  @override
  final bool highPriority;

  @override
  final _CollectionSortingDto sortingDto;

  @override
  String get type => _kCustomCollectionTypeValue;

  @override
  List<Object?> get props {
    return [
      id,
      title,
      lexicalItemIds,
      selected,
      highPriority,
      sortingDto,
    ];
  }

  factory _CustomCollectionDto.fromJson(Map<String, dynamic> json) {
    final lexicalItemIds = BuiltSet<String>.from(json[_kCollectionLexicalItemIdsKey]);

    final sortingDto = _CollectionSortingDto.fromString(json[_kCollectionSortingKey]);

    return _CustomCollectionDto(
      id: json[_kCollectionListItemIdKey],
      title: json[_kCustomCollectionListItemTitleKey],
      lexicalItemIds: lexicalItemIds,
      selected: json[_kCollectionSelectedKey],
      highPriority: json[_kCollectionHighPriorityKey],
      sortingDto: sortingDto,
    );
  }

  factory _CustomCollectionDto.fromEntity(CustomCollection collection) {
    final sortingDto = _CollectionSortingDto.fromEntity(collection.sorting);

    return _CustomCollectionDto(
      id: collection.id,
      title: collection.title,
      lexicalItemIds: collection.lexicalItemIds,
      selected: collection.selected,
      highPriority: collection.highPriority,
      sortingDto: sortingDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kCollectionListItemTypeKey: type,
      _kCollectionListItemIdKey: id,
      _kCustomCollectionListItemTitleKey: title,
      _kCollectionLexicalItemIdsKey: lexicalItemIds.toList(),
      _kCollectionSelectedKey: selected,
      _kCollectionHighPriorityKey: highPriority,
      _kCollectionSortingKey: sortingDto.toString(),
    };
  }

  @override
  CustomCollection toEntity() {
    return CustomCollection(
      id: id,
      title: title,
      lexicalItemIds: lexicalItemIds,
      selected: selected,
      highPriority: highPriority,
      sorting: sortingDto.toEntity(),
    );
  }
}
